//
//  ProgressViewController.m
//  10-10
//
//  Created by 钟纪旺 on 16/11/17.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "ProgressViewController.h"

@interface ProgressViewController ()<NSURLSessionDataDelegate>{

    NSMutableData *Data;


}
@property (weak, nonatomic) IBOutlet UIProgressView *onProgress;
@property (weak, nonatomic) IBOutlet UIButton *onDownload;
@property (weak, nonatomic) IBOutlet UIImageView *onImage;

@end

@implementation ProgressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _onProgress.progressTintColor =[UIColor redColor];
   // _onProgress.frame =CGRectMake(30, 100, 200, 50);

    
}
- (IBAction)onShare:(id)sender {
    
    UIActivityViewController *con = [[UIActivityViewController alloc]initWithActivityItems:@[@"要分享的内容"]applicationActivities:nil];
    [self presentViewController:con animated:YES completion:nil];
    
}
- (IBAction)onAlert:(id)sender {
    UIAlertController *con = [UIAlertController alertControllerWithTitle:@"提示" message:@"你干啥" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        return ;
    }];
    [con addAction:act1];
    [con addAction:act2];
    [self presentViewController:con animated:YES completion:nil];
    
    
    
}
- (IBAction)onProg:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://a4.qpic.cn/psb?/6034200c-4c88-4b61-9633-ca41da8c76d0/x2mYNQn8UycqoKVM3tKOkHx*KKwNmO16uKYGavIf4eE!/b/dGcBAAAAAAAA&ek=1&kp=1&pt=0&bo=OASABwAAAAAFAJg!&sce=60-2-2&rf=viewer_311"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionDataTask *task = [session dataTaskWithURL:url];
    [task resume];
    self.onProgress.progress = 0;
    self.onImage.image = nil;
 
}

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{

    self.onImage.image = [UIImage imageWithData:Data];


}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
    [self.onProgress setProgress:(float)dataTask.countOfBytesReceived/(float)dataTask.countOfBytesExpectedToReceive animated:YES];
     if(!Data){
         Data = [NSMutableData new];
    
    
    }
    [Data appendData:data];


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
